<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addFormVisible = true">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="归属" value="username"></el-option>
              <el-option label="批次" value="batch"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>

    <!-- table -->
    <el-table stripe style="width: 100%" :data="tableData" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="140" align="center" prop="batch" label="批次"></el-table-column>
       <el-table-column v-if="colData[1].istrue" min-width="120" align="center" prop="username" label="归属"></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="110" align="center" prop="default_launcher" label="桌面">
        <template slot-scope="scope">{{scope.row.default_launcher ? scope.row.default_launcher : '系统默认' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="110" align="center" prop="default_browser" label="浏览器">
        <template slot-scope="scope">{{scope.row.default_browser ? scope.row.default_browser : '系统默认' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="110" align="center" prop="default_videoplayer" label="视频播放器">
        <template slot-scope="scope">{{scope.row.default_videoplayer ? scope.row.default_videoplayer : '系统默认' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[5].istrue" min-width="110" align="center" prop="default_musiclayer" label="音乐播放器">
        <template slot-scope="scope">{{scope.row.default_musiclayer ? scope.row.default_musiclayer : '系统默认' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[6].istrue" min-width="110" align="center" prop="default_filemanager" label="文件管理器">
        <template slot-scope="scope">{{scope.row.default_filemanager ? scope.row.default_filemanager : '系统默认' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[7].istrue" min-width="110" align="center" prop="default_gallery" label="图库">
        <template slot-scope="scope">{{scope.row.default_gallery ? scope.row.default_gallery : '系统默认' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[8].istrue" min-width="110" align="center" prop="default_IME" label="输入法">
        <template slot-scope="scope">{{scope.row.default_IME ? scope.row.default_IME : '系统默认' }}</template>
      </el-table-column>
      <el-table-column v-if="colData[9].istrue" prop="create_time" label="创建时间" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.create_time| activateTime}}</template>
      </el-table-column>
      <el-table-column label="操作" min-width="150" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getqueryIdDefaultApp(scope.row.id)">编辑</el-button>
            <el-button size="mini" @click="deleteShow(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>

    <!-- 添加弹窗 -->
    <el-dialog title="系统默认App" :visible.sync="addFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="25%" @close="addFormclose">
      <el-form :model="addForm" :rules="rules" ref="ruleForm" class="diolog_adapt">
        <el-form-item label="选择批次" label-width="120px" prop="batch">
          <el-select v-model="addForm.batch" :multiple-limit="10" placeholder="请选择批次" style="flex:1" multiple>
            <el-option v-for="item in lotoptions" :key="item.id" :label="item.batch" :value="item.id.toString()"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认桌面:" label-width="120px" prop="default_launcher">
          <el-input v-model="addForm.default_launcher" placeholder="默认系统桌面"></el-input>
        </el-form-item>
        <el-form-item label="默认浏览器:" label-width="120px" prop="default_browser">
          <el-input v-model="addForm.default_browser" placeholder="默认系统浏览器"></el-input>
        </el-form-item>
        <el-form-item label="默认视频播放器:" label-width="120px" prop="default_videoplayer">
          <el-input v-model="addForm.default_videoplayer" placeholder="默认系统视频播放器"></el-input>
        </el-form-item>
        <el-form-item label="默认音乐播放器:" label-width="120px" prop="default_musiclayer">
          <el-input v-model="addForm.default_musiclayer" placeholder="默认系统音乐播放器"></el-input>
        </el-form-item>
        <el-form-item label="默认文件管理器:" label-width="120px" prop="default_filemanager">
          <el-input v-model="addForm.default_filemanager" placeholder="默认系统管理器"></el-input>
        </el-form-item>
        <el-form-item label="默认图库:" label-width="120px" prop="default_gallery">
          <el-input v-model="addForm.default_gallery" placeholder="默认系统图库"></el-input>
        </el-form-item>
        <el-form-item label="默认输入法:" label-width="120px" prop="default_IME">
          <el-input v-model="addForm.default_IME" placeholder="默认系统输入法"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" size="mini" round @click="getaddDefaultApp('ruleForm')">确 定</el-button>
        <el-button v-else type="primary" @click="geteditDefaultApp('ruleForm')" size="mini" round>确定</el-button>
      </div>
    </el-dialog>
    <!--添加弹窗 -->

    <!-- 删除弹窗start -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
    <!-- 删除弹窗end -->
  </div>
</template>

<script>
import UploadExcelComponent from "@/components/UploadExcelSn/index.vue";
import dynamicTable from "@/components/DynamicTable";
import { getBatchTwo } from "@/api/equipment.js";
import { queryDefaultApp, queryIdDefaultApp, addDefaultApp, editDefaultApp, deleteDefaultApp } from "@/api/deployment.js";
import deleteDialog from "@/components/deleteDialog";
import tableMini from '@/utils/tableMini.js'
export default {
  mixins: [tableMini],
  components: { UploadExcelComponent, dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["批次", "归属", "桌面", "浏览器", "视频播放器", "音乐播放器", "文件管理器", "图库", "输入法", "创建时间"],
      tableData: [],
      select: "", //搜索条件
      //编辑按钮控制
      editSubjects: true,
      subjectId: 0,
      listQuery: { startSize: 1, pageSize: 10, batch: undefined, username: undefined, userId: localStorage.getItem("ID") },
      addFormVisible: false,
      lotoptions: [],
      addForm: {
        userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), batch: '', default_launcher: '', default_browser: '', default_videoplayer: '', default_musiclayer: '',
        default_filemanager: '', default_gallery: '', default_IME: ''
      },
      rules: { batch: [{ required: true, message: "请选择批次", trigger: "blur" }] }
    }
  },
  created() {
    this.getTabularData()
    this.getBatchTwos()
  },
  methods: {
    //删除按钮事件
    deleteEvents() {
      let params = { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), id: this.deleteDialogs.deleteId }
      deleteDefaultApp(params).then(res => {
        this.$message.success(res.msg)
        this.getTabularData()
      })
    },
    //添加
    getaddDefaultApp(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          for (let i in this.addForm) {
            this.addForm[i] = this.addForm[i].toString().replace(/^\s*|\s*$/g, "")
          }
          this.addForm.batch = this.addForm.batch.toString()
          addDefaultApp(this.addForm).then(res => {
            this.$message.success(res.msg)
            this.getTabularData()
            this.addFormVisible = false
          }).catch(err => { })
        } else {
          return false;
        }
      });
    },
    //ID查询信息
    getqueryIdDefaultApp(id) {
      this.editSubjects = false
      this.subjectId = id
      queryIdDefaultApp({ id: id }).then(({ data }) => {
        Object.keys(this.addForm).forEach(key => this.addForm[key] = data[key])
        this.addForm.batch = data.batch_id.split(',')
        console.log('---------', this.addForm.batch)
        // this.addForm.batch = this.lotoptions.filter(item => {
        //   if (item.batch == data.batch) {
        //     return item.id
        //   }
        // })[0].id
        this.addFormVisible = true
      }).catch(err => { })
    },
    //编辑信息
    geteditDefaultApp(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.last_ip = sessionStorage.getItem("Ip")
          addForm.userId = localStorage.getItem("ID")
          for (let i in addForm) {
            addForm[i] = addForm[i] != null ? addForm[i].toString().replace(/^\s*|\s*$/g, "") : ''
          }
          this.addForm.batch = this.addForm.batch.toString()
          addForm.id = this.subjectId
          editDefaultApp(addForm).then(res => {
            this.$message.success(res.msg)
            this.getTabularData()
            this.addFormVisible = false
          }).catch(err => { })
        } else {
          return false;
        }
      });
    },
    //根据ID获取批次列表
    getBatchTwos() {
      let data = { userId: localStorage.getItem("ID") }
      getBatchTwo(data).then(res => {
        this.lotoptions = res.data
      }).catch(err => {
        this.lotoptions = []
      })
    },
    getTabularData() {
      queryDefaultApp(this.listQuery).then(res => {
        this.tableData = res.data
        this.total = res.total
      }).catch(err => {
        this.tableData = []
        this.total = 0
      })
    },
    //编辑添加表单关闭事件
    addFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        this.editSubjects = true
        this.addForm = this.$options.data().addForm
      })
    }
  }
}
</script>